package com.tianqu.tms.www.car.action;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.www.car.actionform.CarInnerRegisterActionForm;
import com.tianqu.tms.www.car.controller.CarOutRegisterResultController;
import com.tianqu.tms.www.car.viewhelper.CarInnerRegisterViewhelper;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import java.sql.Timestamp;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhangxueliang
 * 出库登记确认action
 */
public class CarOutRegisterResultAction extends TmsAction{
	private Logger logger = Logger.getLogger(CarOutRegisterResultAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		CarInnerRegisterActionForm registerForm = (CarInnerRegisterActionForm) uc.getInputForm();
		CarInnerRegisterViewhelper viewhelper = new CarInnerRegisterViewhelper();
		viewhelper = (CarInnerRegisterViewhelper)uc.getAttribute(TmsConstants.VIEWHELP);		
		request.setAttribute(TmsConstants.VIEWHELP, viewhelper);
		Car car = Car.getInstance();
		//保存按钮逻辑      
        car = car.findCarBySerial(registerForm.getSerial());
        if(car != null){
        	//当这个状态为1的时候车辆被挂起
        	if(car.getBarCodeNum() == 1){
        		//compareTo方法是如果有null的情况下会空指针异常
        		if( car.getTmpEnterTime() != null ){
        			Timestamp ts = new Timestamp(new Date().getTime());
        			//返回1表示大于，返回-1表示小于，返回0表示相等
        			if( ts.compareTo(car.getTmpEnterTime()) != -1 ){
        				setErrorsToRequest(request, TmsConstants.U8001, null);
        				return mapping.getInputForward();//如果临时进出开始时间大于结束时间，证明临时进出时间过期了，车辆是挂起的状态，不能进行任何操作
        												//如果临近进出开始时间等于结束时间，证明临时进出时间到期了，车辆是挂起的状态，不能进行任何操作
        			}
        		}else {
        			setErrorsToRequest(request, TmsConstants.U8001, null);
    				return mapping.getInputForward();//如果临时进出时间为空的话，证明次车辆没有设置临时进出，车辆是被挂起的，不能进行任何操作
        		}
        	}
        	
			if(car.getRegisterStatus() == 2 || car.getRegisterStatus() == 3 
					|| car.getRegisterStatus() == 4 || car.getRegisterStatus() == 6){
				setErrorsToRequest(request, TmsConstants.U8003, null);//车辆库存状态不符合要求，不能进行操作！
				return mapping.getInputForward();
			}
        }
		CarOutRegisterResultController controller = new CarOutRegisterResultController();
		controller.execute(uc);

		// add by huangheli at 20150805 end
		request.setAttribute("tipMessage","出库成功");
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
